home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Mindy / Mindy 1.2 - portable sources / demos / cat / cat.dylan next >
Encoding:
Text File  |  1995-03-15  |  2.2 KB  |  76 lines  |  [TEXT/ttxt]

  1. module: Concatenate
  2. rcs-header: $Header: cat.dylan,v 1.2 94/10/26 19:46:02 nkramer Exp $
  3.  
  4. //======================================================================
  5. //
  6. // Copyright (c) 1994  Carnegie Mellon University
  7. // All rights reserved.
  8. // 
  9. // Use and copying of this software and preparation of derivative
  10. // works based on this software are permitted, including commercial
  11. // use, provided that the following conditions are observed:
  12. // 
  13. // 1. This copyright notice must be retained in full on any copies
  14. //    and on appropriate parts of any derivative works.
  15. // 2. Documentation (paper or online) accompanying any system that
  16. //    incorporates this software, or any part of it, must acknowledge
  17. //    the contribution of the Gwydion Project at Carnegie Mellon
  18. //    University.
  19. // 
  20. // This software is made available "as is".  Neither the authors nor
  21. // Carnegie Mellon University make any warranty about the software,
  22. // its performance, or its conformity to any specification.
  23. // 
  24. // Bug reports, questions, comments, and suggestions should be sent by
  25. // E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  26. //
  27. //======================================================================
  28. //
  29. // This demo demonstrates the streams library by duplicating the unix
  30. // ``cat'' utility.
  31. //
  32. // We need to define our own library because we need to use the Streams
  33. // library in addition to the standard Dylan library.
  34. //
  35.  
  36. define library Concatenate
  37.   use Dylan;
  38.   use Streams;
  39. end;
  40.  
  41. define module Concatenate
  42.   use Dylan;
  43.   use Extensions;
  44.   use Streams;
  45.   use Standard-IO;
  46. end;
  47.  
  48. define method main (argv0, #rest names)
  49.   if (empty?(names))
  50.     spew(*standard-input*);
  51.   else
  52.     for (name in names)
  53.       let stream = if (name = "-")
  54.              make(<fd-stream>, fd: 0);
  55.            else
  56.              make(<file-stream>, name: name);
  57.            end;
  58.       spew(stream);
  59.       close(stream);
  60.     end;
  61.   end;
  62. end;
  63.  
  64. define method spew (stream :: <stream>)
  65.   let (buf, next, stop) = get-input-buffer(stream);
  66.   if (next ~= stop)
  67.     write(buf, *standard-output*, start: next, end: stop);
  68.   end;
  69.   for (stop = fill-input-buffer(stream, 0)
  70.      then fill-input-buffer(stream, 0),
  71.        until stop = 0)
  72.     write(buf, *standard-output*, start: 0, end: stop);
  73.   end;
  74.   release-input-buffer(stream, 0, 0);
  75. end;
  76.